package com.servlet;

import com.dao.LoginDao;
import com.dao.imp.LoginDaoImp;
import com.entity.User;
import com.sun.net.httpserver.HttpContext;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/20 21:33
 **/

@WebServlet(name = "LoginStatusServlet", value = "/LoginStatusServlet")
public class LoginStatusServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        Map userStatusMap = (Map) application.getAttribute("userStatusMap");
        LoginDao loginResult = new LoginDaoImp();
        String jsonString = "{status:1}";
        Object userId = session.getAttribute("userId");
        if (userId !=null){
            int id = Integer.parseInt(userId.toString());
            User user = (User) userStatusMap.get(id+"nickname");
            jsonString = "{status:"+userStatusMap.get(id)+","+"username:\""+user.getUser_nickname()+"\"}";
        }

            JSONObject json = JSONObject.fromObject(jsonString);

        response.getWriter().print(json);
    }
}
